package com.major;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Provider {
    @Test
    public void testSendMessage() {
        // 连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 主机地址
        connectionFactory.setHost("192.168.0.128");
        // 端口号
        connectionFactory.setPort(5672);
        // 连接哪个虚拟主机
        connectionFactory.setVirtualHost("/ems");
        // 用户名&密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");

        // 获取连接对象
        Connection connection = null;
        // 获取通道
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            // 通道绑定消息队列
            // s:建立通道   b: 持久化     b1：独占队列  b2：消费完成后自动删除队列     map：额外参数
            channel.queueDeclare("hello2",false,false,false,null);
            // 发布消息
            channel.basicPublish("","hello2",null,"hello rabbitmq22222".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }


//        channel.close();
//        connection.close();
    }
}
